package com.catt.orderdinner.controller;

import com.catt.orderdinner.dataobject.TbUser;
import com.catt.orderdinner.form.ApiReturnObj;
import com.catt.orderdinner.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@ResponseBody
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("login")
    public ApiReturnObj login(String username,HttpServletResponse response){
        ApiReturnObj apiReturnObj = new ApiReturnObj();
        TbUser user = userService.login(username);
        if (user == null) {
            apiReturnObj.setCode("300");
            apiReturnObj.setMessage("请先进行注册");
        } else {
            apiReturnObj.setCode("200");
            apiReturnObj.setMessage("登陆成功");
            apiReturnObj.setObject(user);
        }
        return apiReturnObj;
    }

    @RequestMapping("register")
    public String register(String username,String projectName ,String projectNum){
        TbUser login = userService.login(username);
        if (login!=null){
            // 用户名不能重复
            return "namerepeat";
        }else {
            TbUser tbUser = new TbUser();
            tbUser.setUserName(username);
            userService.save(tbUser);
            return "ok";
        }


    }
}
